home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c++
- Subject: Re: Q Re: struct and functions
- Date: 3 Feb 1996 10:01:18 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4evbpe$btq@news1.usa.pipeline.com>
- NNTP-Posting-Host: 38.8.120.9
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Feb 03, 1996 09:18:27 in article <Q Re: struct and functions>,
- 'martp@oanet.com' wrote:
-
-
- >Hi
- >
- >I'm a second semister student in Computer Systems, and have a question
- >about passing structs, for Win programming.
-
- It's the same in Windows and in character-mode systems.
-
- >I have a struct DATA containing, amongst other things, HWND, and
- >COLORREF.
- >How do I define, call, and make a header for a function, so that I
- >can just pass the DATA to the function, and separate the HWND and
- >COLORREF in the header?
- >
- >struct DATA {
- >HWND hWnd;
- >COLORREF cColor;
- >}stData;
- >
- The problem with the above is that it actually instantiates the
- structure. If you include this header in multiple source files --
- which you nearly always do in "real" programs -- then you
- get multiple definition linker errors. Better to change it to:
- (You posted this to comp.lang.c++, but judging by your
- code, you want a "straight C" answer.):
-
- typedef struct tagDATA
- {
- HWND hWnd;
- COLORREF cColor;
- } DATA;
-
- >void fnChangeData( DATA stData );
- >
- >fnChangeColor( stData );
-
- Change the above line to:
- void fnChangeColor(DATA * pdata);
-
- All of the above goes into a header file. From here on down,
- into a .c file:
- >
- >
- >void fnChangeColor( HWND hWnd, COLORREF cColor )
- No, this doesn't match the declaration in the header. Define
- it just like its declaration:
-
- void fnChangeColor(DATA * data)
-
- >{
- Now, inside this function you refer to elements of the
- structure using pointer syntax; e.g.,
-
- HDC hDC = GetDC(data->hWnd);
- SetTextColor(hDC, data->cColor);
-
- >
- >return;
- Not necessary.
- >}
- >
-
- To call this mess, do something like:
-
- int foo (HWND hw)
- {
- DATA d; /* this reserves memory on the stack for the struct */
- /* of course you could initialize the struct with . = { hw, RGB(255, 0,
- 0) };*/
- d.hWnd = hw;
- d.cColor = RGB(255, 0, 0);
- fnChangeColor(&d); /* pass the address of your structure */
-
- --
- Pete Grant
- Kalevi, Inc.
- Object Oriented Software Development
-